package com.block.service.impl;
//部落内部操作业务
import com.block.common.Msg;
import com.block.dao.BlockUserMapper;
import com.block.entity.BlockUser;
import com.block.service.BlockUserService;
import com.block.utils.SmallUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class BlockUserImpl implements BlockUserService {

    @Resource
    private BlockUserMapper blockUserMapper;
    //实例工具类
    private SmallUtils utils=new SmallUtils();

    //获取某个部落的总贡献值
    @Override
    public int getActiveCount(BlockUser user) {
        List<BlockUser> all = blockUserMapper.getAll(user);
        int activeCount = 0;//存储总贡献值
        for (BlockUser item :
                all) {
            activeCount += item.getActiveCount();
        }
        return activeCount;
    }
    //关注某个部落
    @Override
    public Msg onFollowBlock(BlockUser user) {
        Msg msg=new Msg();
        BlockUser newUser=blockUserMapper.getOneBlockUser(user);//获取此人在此部落有没有之前关注的信息，是否之前就关注了此部落，
        if (newUser!=null){//如果以前关注过此部落，在此修改此FollowType（是否关注0/1）为0，也就是关注
            newUser.setFollowType(0);//0关注  1不关注
            blockUserMapper.updateByPrimaryKeySelective(newUser);
            msg.setMsg("欢迎再次回来~>_<~");
        }else {
            //部落人员表没有此人的数据，初始化此人的一些信息然后插入到部落人员表
            user.setId(utils.uuid());
            user.setFollowType(0);
            user.setCreateTime(utils.getNowDate());
            user.setActiveCount(0);
            if (blockUserMapper.insertSelective(user)>0){
                msg.setMsg("欢迎加入~>_<~");
            }else {
                msg.setCode(-1);
                msg.setMsg("出现错误，进入失败");
            }
        }
        return msg;
    }
    // 取消关注某个部落块
    @Override
    public Msg offFollowBlock(BlockUser user) {
        Msg msg=new Msg();
        BlockUser newUser=blockUserMapper.getOneBlockUser(user);//获取此人在此部落有没有之前关注的信息，是否之前就关注了此部落，
        if (newUser!=null){//如果以前关注过此部落，在此修改此FollowType（是否关注0/1）为0，也就是关注
            newUser.setFollowType(1);//0关注  1不关注
            blockUserMapper.updateByPrimaryKeySelective(newUser);
            msg.setMsg("已取消");
        }else {
            msg.setMsg("出现错误，本来就没有关注此部落");
        }
        return msg;
    }
    //某人在某部落的信息，贡献值，加入时间，是否关注一类的一类的
    @Override
    public Msg getOneBlockUser(BlockUser user) {
        Msg msg=new Msg();
        BlockUser newUser=blockUserMapper.getOneBlockUser(user);//获取此人在此部落的信息，是否关注了此部落
        //List<BlockUser> all = blockUserMapper.getAll(user);
        if (newUser!=null){//已经关注了此部落
            msg.setData(newUser);
        }else {
            msg.setMsg("还没有关注此板块，快来加入吧");
        }
        return msg;
    }
    //获取某个部落里人的贡献值的排行榜，
    @Override
    public Msg getRankings(BlockUser user) {
        Msg msg=new Msg();
        List<BlockUser> all = blockUserMapper.getAll(user);
        if (all!=null){
            msg.setData(all);//存放
        }else if (all==null){
            msg.setCode(1);
            msg.setMsg("空空如也 T∧T");
        }
        return msg;
    }

}
